package com.widget.wallpaper;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class WNWConfigure extends Activity {
	private static final String PREFS_NAME = "org.androidappdev.batterywidget.BatteryAppWidgetProvider";
	private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
//	private boolean mShowTemperature = false;
//	private int mDegrees = -1;
	private String mCharacter = "penguin";

	// Preferences
//	private static final String PREF_SHOW_TEMPERATURE = "show_temperature";
//	private static final String PREF_DEGREES = "degrees";
	private static final String PREF_CHAR_KEY = "character";
//	public static final int PREF_CELSIUS = 0;
//	public static final int PREF_FAHRENHEIT = 1;
	public static final String PREF_CHARACTER = "penguin";
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configure);

		
		
		// Find the widget id from the intent.
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		Log.d("check_point","extra"+extras);
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			
		}

		// Bind the action for the save button.
		findViewById(R.id.save_button).setOnClickListener(mOnClickListener);

//		final CheckBox checkbox = (CheckBox) findViewById(R.id.cb_show_temp);
//		checkbox.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				// Perform action on clicks, depending on whether it's now
//				// checked
//				final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg_temp_opts);
//				if (((CheckBox) v).isChecked()) {
//					mShowTemperature = true;
//					radioGroup.setVisibility(View.VISIBLE);
//				} else {
//					mShowTemperature = false;
//					radioGroup.setVisibility(View.GONE);
//				}
//			}
//		});

//		final RadioButton radioCelsius = (RadioButton) findViewById(R.id.radio_celsius);
//		final RadioButton radioFahrenheit = (RadioButton) findViewById(R.id.radio_fahrenheit);
		final RadioButton radioPenguin = (RadioButton) findViewById(R.id.character1);
		final RadioButton radioWhite = (RadioButton) findViewById(R.id.character0);
		final RadioButton radioBlack = (RadioButton) findViewById(R.id.character2);
		
		final Context context = WNWConfigure.this;
		String currentCharacter = WNWConfigure.getCharacterPref(context);
		if(currentCharacter.equalsIgnoreCase("penguin"))
		{
			radioPenguin.setChecked(true);
			radioWhite.setChecked(false);
			radioBlack.setChecked(false);
			mCharacter = "penguin";
		}
		if(currentCharacter.equalsIgnoreCase("white"))
		{
			radioPenguin.setChecked(false);
			radioWhite.setChecked(true);
			radioBlack.setChecked(false);
			mCharacter = "white";
		}
		if(currentCharacter.equalsIgnoreCase("black"))
		{
			radioPenguin.setChecked(false);
			radioWhite.setChecked(false);
			radioBlack.setChecked(true);
			mCharacter = "black";
		}
		
		//blinding radio buttons
		radioPenguin.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(radioPenguin.isChecked())
				{
					radioWhite.setChecked(false);
					radioBlack.setChecked(false);
					mCharacter = "penguin";
				}
			}
		});
		radioWhite.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(radioWhite.isChecked())
				{
					radioPenguin.setChecked(false);
					radioBlack.setChecked(false);
					mCharacter = "white";
				}
			}
		});
		radioBlack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(radioBlack.isChecked())
				{
					radioPenguin.setChecked(false);
					radioWhite.setChecked(false);
					mCharacter = "black";
				}
			}
		});
		//End blinding
//		radioCelsius.setOnClickListener(mRadioListener);
//		radioFahrenheit.setOnClickListener(mRadioListener);

		// If they gave us an intent without the widget id, just bail.
//		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
//			finish();
//		}

	}

	private OnClickListener mOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			final Context context = WNWConfigure.this;

			// When the button is clicked, save our prefs and return that they
			// clicked OK.
//			saveTemperaturePref(context, mAppWidgetId, mShowTemperature,
//					mDegrees,mCharacter);
			saveCharacterPref(context, mAppWidgetId, mCharacter);

			// Push widget update to surface with newly set prefs
			context.startService(new Intent(context, WNWService.class));

			// Make sure we pass back the original appWidgetId
			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					mAppWidgetId);
			setResult(RESULT_OK, resultValue);
			finish();
			Log.d("charater",mCharacter);
		}
	};

//	private OnClickListener mRadioListener = new OnClickListener() {
//		public void onClick(View v) {
//			RadioButton rb = (RadioButton) v;
//			mDegrees = rb.getId() == R.id.radio_celsius ? PREF_CELSIUS
//					: PREF_FAHRENHEIT;
//		}
//	};
	

//	private void saveTemperaturePref(Context context, int appWidgetId,
//			boolean show, int degrees, String character) {
//		SharedPreferences.Editor prefs = context.getSharedPreferences(
//				PREFS_NAME, 0).edit();
//		prefs.putBoolean(PREF_SHOW_TEMPERATURE, show);
//		if (show) {
//			prefs.putInt(PREF_DEGREES, degrees);
//		}
//		prefs.putString(PREF_CHAR_KEY, character);
//		prefs.commit();
//	}
	private void saveCharacterPref(Context context, int appWidgetId, String character) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putString(PREF_CHAR_KEY, character);
		prefs.commit();
	}

//	public static boolean getTemperaturePref(Context context) {
//		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
//		return prefs.getBoolean(PREF_SHOW_TEMPERATURE, false);
//	}
//
//	public static int getDegreesPref(Context context) {
//		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
//		return prefs.getInt(PREF_DEGREES, PREF_CELSIUS);
//	}
	public static String getCharacterPref(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		return prefs.getString(PREF_CHAR_KEY, PREF_CHARACTER);
	}

}
